﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Windows.Forms;

namespace EntLibContrib.Query.QuickStart
{
	/// <summary>
	/// The main QAB Test Form
	/// </summary>
	public partial class QueryTesterForm : Form
	{
		#region Fields
		private DataSet control;
		private Guid identityId = new Guid("816dae3c-9bbf-4c5f-9301-bc52575dcf09");
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="QueryTesterForm"/> class.
		/// </summary>
		public QueryTesterForm()
		{
			InitializeComponent();
			InitializeDataSet();
		}
		#endregion

		#region Button Event handlers
		private void btnNewId_Click(object sender, EventArgs e)
		{
			txtId.Text = Guid.NewGuid().ToString();
		}

		private void btnGetId_Click(object sender, EventArgs e)
		{
			DataSet ds = (DataSet)dataGridView1.DataSource;
			if (ds != null)
			{
				txtId.Text = ds.Tables[0].Rows[0]["Id"].ToString();
			}
		}

		private void btnGetOwnerId_Click(object sender, EventArgs e)
		{
			DataSet ds = (DataSet)dataGridView1.DataSource;
			if (ds != null)
			{
				txtOwnerId.Text = ds.Tables[0].Rows[0]["OwnerId"].ToString();
			}
		}
		#endregion

		#region Read Event handlers
		private void rdoControl_Click(object sender, EventArgs e)
		{
			BindToGrid(control.Copy());
		}

		private void rdoUser_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Identity");
			DataSet ds = query.ExecuteForRead();
			ds.Tables[0].TableName = "Identity";
			BindToGrid(ds);
		}

		private void rdoOwner_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Owner");
			DataSet ds = query.ExecuteForRead();
			ds.Tables[0].TableName = "Owner";
			BindToGrid(ds);
		}

		private void rdoBasic1_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic1");
			DataSet ds = query.ExecuteForRead();
			ds.Tables[0].TableName = "Basic1";
			BindToGrid(ds);
		}

		private void rdoBasic1All_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic1 All");
			DataSet ds = query.ExecuteForRead();
			ds.Tables[0].TableName = "Basic1";
			BindToGrid(ds);
		}

		private void rdoBasic1ById_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
				MessageBox.Show("You must enter a valid Id in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic1 By Id");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("Id", new Guid(txtId.Text)));
				ds.Tables[0].TableName = "Basic1";
				BindToGrid(ds);
			}
		}

		private void rdoBasic1BySampleString_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtSampleString.Text))
				MessageBox.Show("You must enter a valid sample string in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic1 By SampleString");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("SampleString", txtSampleString.Text));
				ds.Tables[0].TableName = "Basic1";
				BindToGrid(ds);
			}
		}

		private void rdoBasic1ObjectSort_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic1 Object Sort");
			DataSet ds = query.ExecuteForRead();
			ds.Tables[0].TableName = "Basic1";
			BindToGrid(ds);
		}
		private void rdoBasic2Wcf_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic2");
			DataSet ds = query.ExecuteForRead();
			BindToGrid(ds);
		}

		private void rdoBasic2AllWcf_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic2 All");
			DataSet ds = query.ExecuteForRead();
			BindToGrid(ds);
		}

		private void rdoBasic2ByIdWcf_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
				MessageBox.Show("You must enter a valid Id in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic2 By Id");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("id", new Guid(txtId.Text)));
				BindToGrid(ds);
			}
		}

		private void rdoBasic2ObjectById_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
				MessageBox.Show("You must enter a valid Id in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic2 Object By Id");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("id", new Guid(txtId.Text)));
				BindToGrid(ds);
			}
		}

		private void rdoBasic2ByNameWcf_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtName.Text))
				MessageBox.Show("You must enter a valid Name (or part) in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic2 By Name");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("name", txtName.Text));
				BindToGrid(ds);
			}
		}

		private void rdoBasic3Object_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic3 Object");
			DataSet ds = query.ExecuteForRead();
			BindToGrid(ds);
		}

		private void rdoBasic3All_Click(object sender, EventArgs e)
		{
			IQuery query = QueryFactory.CreateQuery("Read Basic3 All");
			DataSet ds = query.ExecuteForRead();
			BindToGrid(ds);
		}

		private void rdoBasic3ById_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
				MessageBox.Show("You must enter a valid Id in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic3 By Id");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("Id", new Guid(txtId.Text)));
				BindToGrid(ds);
			}
		}

		private void rdoBasic3ObjectByName_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtName.Text))
				MessageBox.Show("You must enter a valid Name in the text box provided");
			else
			{
				IQuery query = QueryFactory.CreateQuery("Read Basic3 Object By Name");
				DataSet ds = query.ExecuteForRead(CreateSimpleParameterSet("Name", txtName.Text));
				BindToGrid(ds);
			}
		}
		#endregion

		#region Write Event handlers
		private void rdoBasic1Create_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtSampleString.Text))
			{
				MessageBox.Show("You must enter a valid sample string in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtSampleDate.Text))
			{
				MessageBox.Show("You must enter a valid sample date in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtSampleInt.Text))
			{
				MessageBox.Show("You must enter a valid sample integer in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtSampleDecimal.Text))
			{
				MessageBox.Show("You must enter a valid sample decimal in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtOwnerId.Text))
			{
				MessageBox.Show("You must enter a valid Owner Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Create Basic1").ExecuteForWrite(CreateBasic1CreateParameterDataSet(
																																new Guid(txtId.Text),
																																txtSampleString.Text,
																																DateTime.Parse(txtSampleDate.Text, DateTimeFormatInfo.CurrentInfo),
																																cbSampleBoolean.Checked,
																																Int32.Parse(txtSampleInt.Text, NumberFormatInfo.CurrentInfo),
																																Decimal.Parse(txtSampleDecimal.Text, NumberFormatInfo.CurrentInfo),
																																new Guid(txtOwnerId.Text)));
			MessageBox.Show("the Basic1 Data has been created");
		}

		private void rdoBasic1Update_Click(object sender, EventArgs e)
		{
			DataSet ds = (DataSet)dataGridView1.DataSource;
			if (ds.Tables[0].TableName != "Basic1")
				MessageBox.Show("You must have the Basic1 Dataset on display to use this command");
			else
			{
				foreach (DataRow row in ds.Tables[0].Rows)
				{
					QueryFactory.CreateQuery("Update Basic1").ExecuteForWrite(CreateBasic1UpdateParameterDataSet(row));
				}
				MessageBox.Show("the Basic1 Data has been updated");
			}
		}

		private void rdoBasic1Delete_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Delete Basic1").ExecuteForWrite(CreateBasic1DeleteParameterDataSet(new Guid(txtId.Text)));
			MessageBox.Show("the Basic1 Data has been deleted");
		}

		private void rdoBasic2CreateWcf_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtName.Text))
			{
				MessageBox.Show("You must enter a valid Name in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtDescription.Text))
			{
				MessageBox.Show("You must enter a valid Description in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtOwnerId.Text))
			{
				MessageBox.Show("You must enter a valid Owner Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Create Basic2").ExecuteForWrite(CreateBasic2CreateParameterDataSet(new Guid(txtId.Text), txtName.Text, txtDescription.Text, new Guid(txtOwnerId.Text)));
			MessageBox.Show("the Basic2 Data has been created");
		}

		private void rdoBasic2UpdateWcf_Click(object sender, EventArgs e)
		{
			DataSet ds = (DataSet)dataGridView1.DataSource;
			if (ds.Tables[0].TableName != "Basic2")
				MessageBox.Show("You must have the Basic2 Dataset on display to use this command");
			else
			{
				foreach (DataRow row in ds.Tables[0].Rows)
				{
					QueryFactory.CreateQuery("Update Basic2").ExecuteForWrite(CreateBasic2UpdateParameterDataSet(row));
				}
				MessageBox.Show("the Basic2 Data has been updated");
			}
		}

		private void rdoBasic2DeleteWcf_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Delete Basic2").ExecuteForWrite(CreateBasic2DeleteParameterDataSet(new Guid(txtId.Text)));
			MessageBox.Show("the Basic2 Data has been deleted");
		}

		private void rdoBasic3Create_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtName.Text))
			{
				MessageBox.Show("You must enter a valid Name in the text box provided");
				return;
			}

			if (String.IsNullOrEmpty(txtOwnerId.Text))
			{
				MessageBox.Show("You must enter a valid Owner Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Create Basic3 Object").ExecuteForWrite(CreateBasic3CreateParameterDataSet(new Guid(txtId.Text), txtName.Text, new Guid(txtOwnerId.Text)));
			MessageBox.Show("the Basic3 Data has been created");
		}

		private void rdoBasic3Update_Click(object sender, EventArgs e)
		{
			DataSet ds = (DataSet)dataGridView1.DataSource;
			if (ds.Tables[0].TableName != "Basic3")
				MessageBox.Show("You must have the Basic3 Dataset on display to use this command");
			else
			{
				foreach (DataRow row in ds.Tables[0].Rows)
				{
					QueryFactory.CreateQuery("Update Basic3").ExecuteForWrite(CreateBasic3UpdateParameterDataSet(row));
				}
				MessageBox.Show("the Basic3 Data has been updated");
			}
		}

		private void rdoBasic3Delete_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(txtId.Text))
			{
				MessageBox.Show("You must enter a valid Id in the text box provided");
				return;
			}
			QueryFactory.CreateQuery("Delete Basic3 Object").ExecuteForWrite(CreateBasic3DeleteParameterDataSet(new Guid(txtId.Text)));
			MessageBox.Show("the Basic3 Data has been deleted");
		}
		#endregion

		#region Private Methods
		private IDictionary<string, object> CreateBasic1CreateParameterDataSet(Guid id, string sampleString, DateTime sampleDate, bool sampleBoolean, int sampleInt, decimal sampleDecimal, Guid ownerId)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", id);
			parameters.Add("SampleString", sampleString);
			parameters.Add("SampleDate", sampleDate);
			parameters.Add("SampleBoolean", sampleBoolean);
			parameters.Add("SampleInt", sampleInt);
			parameters.Add("SampleDecimal", sampleDecimal);
			parameters.Add("OwnerId", ownerId);
			parameters.Add("IdentityId", identityId);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic1UpdateParameterDataSet(DataRow row)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", row["Id"]);
			parameters.Add("SampleString", row["SampleString"]);
			parameters.Add("SampleDate", row["SampleDate"]);
			parameters.Add("SampleBoolean", row["SampleBoolean"]);
			parameters.Add("SampleInt", row["SampleInt"]);
			parameters.Add("SampleDecimal", row["SampleDecimal"]);
			parameters.Add("OwnerId", row["OwnerId"]);
			parameters.Add("IdentityId", identityId);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic1DeleteParameterDataSet(Guid id)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", id);
			parameters.Add("IdentityId", identityId);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic2CreateParameterDataSet(Guid id, string name, string description, Guid ownerId)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", id);
			parameters.Add("Name", name);
			parameters.Add("Description", description);
			parameters.Add("OwnerId", ownerId);
			parameters.Add("CreatedIdentityId", identityId);
			parameters.Add("CreatedDate", DateTime.Now);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic2UpdateParameterDataSet(DataRow row)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", (row["Id"] == null ? Guid.Empty : new Guid((string)row["Id"])));
			parameters.Add("Name", row["Name"]);
			parameters.Add("Description", row["Description"]);
			parameters.Add("OwnerId", (row["OwnerId"] == null ? Guid.Empty : new Guid((string)row["OwnerId"])));
			parameters.Add("UpdatedIdentityId", identityId);
			parameters.Add("UpdatedDate", DateTime.Now);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic2DeleteParameterDataSet(Guid id)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("id", id);
			parameters.Add("deletedIdentityId", identityId);
			parameters.Add("deletedDate", DateTime.Now);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic3CreateParameterDataSet(Guid id, string name, Guid ownerId)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", id);
			parameters.Add("Name", name);
			parameters.Add("OwnerId", ownerId);
			parameters.Add("CreatedIdentityId", identityId);
			parameters.Add("CreatedDate", DateTime.Now);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic3UpdateParameterDataSet(DataRow row)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", (row["Id"] == null ? Guid.Empty : new Guid((string)row["Id"])));
			parameters.Add("Name", row["Name"]);
			parameters.Add("OwnerId", (row["OwnerId"] == null ? Guid.Empty : new Guid((string)row["OwnerId"])));
			parameters.Add("UpdatedIdentityId", identityId);
			parameters.Add("UpdatedDate", DateTime.Now);

			return parameters;
		}

		private IDictionary<string, object> CreateBasic3DeleteParameterDataSet(Guid id)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("Id", id);
			parameters.Add("DeletedIdentityId", identityId);
			parameters.Add("DeletedDate", DateTime.Now);

			return parameters;
		}

		private void BindToGrid(DataSet ds)
		{
			if (ds.Tables.Count > 0)
			{
				dataGridView1.DataSource = ds;
				dataGridView1.DataMember = ds.Tables[0].TableName;
				dataGridView1.Columns[dataGridView1.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			}
			else
				dataGridView1.DataSource = dataGridView1.DataMember = null;
		}

		private void InitializeDataSet()
		{
			control = new DataSet();
			control.Locale = CultureInfo.CurrentCulture;
			control.Tables.Add("Control");
			control.Tables["Control"].Columns.Add("Id", typeof(int));
			control.Tables["Control"].Columns.Add("Name", typeof(string));
			control.Tables["Control"].Columns.Add("Description", typeof(string));
			control.Tables["Control"].Rows.Add(new object[] { 1, "Control", "This is a control test for the tester to display a DataSet not built from the QAB" });
			control.Tables["Control"].Rows.Add(new object[] { 2, "Identity", "This is a list of computer identities stored in a SQL Server database" });
			control.Tables["Control"].Rows.Add(new object[] { 3, "Owner", "This is a list of object owners stored in a SQL Server database" });
			control.Tables["Control"].Rows.Add(new object[] { 4, "Basic1", "The data for this class is stored in a SQL Server database" });
			control.Tables["Control"].Rows.Add(new object[] { 5, "Basic2", "The data for this class is accessed via a WCF service" });
			control.Tables["Control"].Rows.Add(new object[] { 6, "Basic3", "The data for this class is stored in an XML text file" });
			control.Tables["Control"].Rows.Add(new object[] { 7, "*Object*", "Any option with the word 'Object' in it implies a custom command is being used" });
			control.Tables["Control"].Rows.Add(new object[] { 8, "*All*", "Any option with the word 'All' in it will return all rows including the deleted ones" });
			control.Tables["Control"].Rows.Add(new object[] { 9, "*By Id", "These commands need a Guid 'identifier' parameter to filter the search" });
			control.Tables["Control"].Rows.Add(new object[] { 10, "*By Name", "These commands need a string 'name' parameter to filter the search" });
			control.Tables["Control"].Rows.Add(new object[] { 11, "*By Sample Str", "These commands need a 'string' parameter to filter the search" });
			control.Tables["Control"].Rows.Add(new object[] { 12, "*Create", "Create commands need a Guid 'identifier', a Guid 'ownerId' and at leaast one other parameter like 'name'" });
			control.Tables["Control"].Rows.Add(new object[] { 13, "*Update", "Update commands work on the entire DataSet updating every row" });
			control.Tables["Control"].Rows.Add(new object[] { 14, "*Delete", "Delete commands need a Guid 'identifier'" });
		}

		private static IDictionary<string, object> CreateSimpleParameterSet(string parameterName, object parameterValue)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add(parameterName, parameterValue);

			return parameters;
		}
		#endregion
	}
}
